import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import base from '@src/store/base'
import member from '@src/store/member';
import router from '@src/router'
import {
  parseRechargeAwardRule,
} from "@src/common/memberCardUtils";
import { getCategoryAvailable, getMemberMineInfo } from '@src/api/member'
Component({
  /**
   * 组件的属性列表
   */
  behaviors: [storeBindingsBehavior],
  storeBindings: [
    {
      store: base,
      fields: ['themeColor', 'themeTitleColor', 'isiPhoneX']
    },
    {
      store: member,
      fields: ['isMember', 'categoryName', 'categoryId']
    }
  ],
  properties: {
    modules: {
      type: Array,
      value: "",
    },
    login: {
      type: Boolean
    }
  },
  options: {
    multipleSlots: true,
    addGlobalClass: true
  },
  lifetimes: {
    attached: function () {
      if (this.data.isMember) {
        this.setData({ categoryNameMy: categoryName })
      }
      let modulesData = this.data.modules[0];
      this.setData({ pic: modulesData.pic });
      this.initData()
    },
    ready() {
      let self = this;
      setTimeout(() => {
        let query = wx.createSelectorQuery().in(self);
        query.selectAll('.card-award-item').boundingClientRect()
        query.exec(function (res) {
          let heights = []
          res[0].forEach((item => {
            heights.push(item.height)
          }))
          self.setData({ heights });
        })
      }, 1000);
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    rechargeAward: [],
    activeIndex: 0,
    showRechargeAward: [],
    heights: [],
    categoryNameMy: "",
    pic: null,
  },
  /**
   * 组件的方法列表
   */
  methods: {
    initData() {
      getCategoryAvailable().then(res => {
        let item = res.data[0];
        if (this.data.isMember) { res.data.forEach((val, index) => { if (val.category.id == this.data.categoryId) { item = val; } }) }
        this.setData({ categoryNameMy: item.category.categoryName })
        let rechargeAward = parseRechargeAwardRule(item.rechargeAward, false);
        console.log("rechargeAward", rechargeAward)
        let showRechargeAward = [];
        rechargeAward.forEach(item => { showRechargeAward.push(item.rechargeAwardRights) })
        this.setData({ rechargeAward: rechargeAward, showRechargeAward });
        console.log("this.data.showRechargeAward", this.data.showRechargeAward)
      })
    },
    //监听轮播图滚蛋
    bindChange(eventhandle) {
      let index = eventhandle.detail.current;
      this.setData({ activeIndex: index })
    },
    openBalance() {
      if (!this.data.login) { this.triggerEvent('login'); return; }
      if (this.data.isMember) { router.push({ path: 'pages/balance/main' }) }
      else {
        wx.showModal({
          title: '提示',
          content: '您还不是会员噢',
          confirmText: "去开卡",
          success(res) { if (res.confirm) { router.push({ name: 'indexChooseCard' }) } }
        })
      }
    },
    rechargeTap(e) {
      if (!this.data.login) { this.triggerEvent('login'); return; }
      if (this.data.isMember) { router.push({ name: 'member/balanceRecharge' }) }
      else {
        wx.showModal({
          title: '提示',
          content: '您还不是会员噢',
          confirmText: "去开卡",
          success(res) { if (res.confirm) { router.push({ name: 'indexChooseCard' }) } }
        })
      }
    }
  }
})
